package com.cgsd.heyu.ui.task;

import android.content.Context;
import android.content.Intent;
import android.util.TypedValue;
import android.view.View;

import com.cgsd.heyu.R;
import com.cgsd.heyu.api.ApiService;
import com.cgsd.heyu.http.BaseResponse;
import com.cgsd.heyu.http.BaseRxSubscribe;
import com.cgsd.heyu.http.ErrorMessage;
import com.cgsd.heyu.http.RetrofitServiceManagerPhp;
import com.cgsd.heyu.http.RxjavaHelper;
import com.cgsd.heyu.presenter.BasePresenter;
import com.cgsd.heyu.ui.BaseActivity;
import com.cgsd.heyu.ui.BaseFragment;
import com.cgsd.heyu.ui.task.adapter.TabAdapter;
import com.cgsd.heyu.ui.task.vo.PublishDetailVo;
import com.cgsd.heyu.utils.RequestParams;
import com.cgsd.heyu.utils.StatusBarUtil;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;
import okhttp3.RequestBody;

/**
 * Author: sky on 2021/3/5 14:04
 * Email: xcode126@126.com
 * Desc: 我的发布-任务详情
 */
@Deprecated
public class PublishDetailActivity extends BaseActivity<BasePresenter> {

    private ViewPager mViewPager;
    private MagicIndicator mIndicator;

    private TabAdapter adapter;
    private String[] titles;

    private int tid;
    public static Intent getIntent(Context context,int tid) {
        return new Intent(context, PublishDetailActivity_v2.class).putExtra("tid",tid);
    }

    @Override
    protected void setStatusBar() {
        StatusBarUtil.fullBar(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_publish_detail;
    }

    @Override
    protected void initView() {
        tid=getIntent().getIntExtra("tid",0);
        mViewPager = findViewById(R.id.mViewPager);
        mIndicator = findViewById(R.id.mIndicator);
        titles = getResources().getStringArray(R.array.a_publish_detail_title);
        adapter = new TabAdapter(getSupportFragmentManager(), getFragments());
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(titles.length);

        initTitleIndicator();
    }

    @Override
    protected void initData() {
        getPublishDetail();
    }

    /**
     * 初始化菜单栏
     */
    private void initTitleIndicator() {
        CommonNavigator titleNavigator = new CommonNavigator(this);
        titleNavigator.setAdjustMode(true);//true 自适应居中、false 反之
        titleNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(titles[index]);
                simplePagerTitleView.setPadding(0, 0, 0, 0);
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                simplePagerTitleView.setNormalColor(ContextCompat.getColor(context, R.color.textMain));
                simplePagerTitleView.setSelectedColor(ContextCompat.getColor(context, R.color.app_color));
                simplePagerTitleView.setText(titles[index]);

                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setColors(ContextCompat.getColor(context, R.color.app_color));
                return linePagerIndicator;
            }
        });
        mIndicator.setNavigator(titleNavigator);
        ViewPagerHelper.bind(mIndicator, mViewPager);
    }

    /**
     * get fragments
     *
     * @return
     */
    private List<BaseFragment> getFragments() {
        List<BaseFragment> list = new ArrayList<>();
        list.add(WaitCheckFragment.newInstance());
        list.add(YetCheckFragment.newInstance());
        return list;
    }

    /**
     * get Task list
     */
    public void getPublishDetail() {
        showLoading();
        RequestBody requestBody = new RequestParams()
                .put("page", page)
                .put("tid", 400)//分类ID,1为我的任务,2为我的发布,默认1
                .putUid()
                .get();
        RetrofitServiceManagerPhp.getInstance().creat(ApiService.class)
                .getPublishDetail(requestBody)
                .compose(RxjavaHelper.observeOnMainThread())
                .subscribe(new BaseRxSubscribe<BaseResponse<PublishDetailVo>>() {
                    @Override
                    public void onSuccess(BaseResponse<PublishDetailVo> baseResponse) {
                        processPublishDetail(baseResponse.getBizBody());
                        loadSuccess();
                    }

                    @Override
                    public void onError(ErrorMessage error) {
                        setErrorMessage(error);
                        loadSuccess();
                    }
                });
    }

    /**
     * process publish detail
     * @param bizBody
     */
    private void processPublishDetail(PublishDetailVo bizBody) {

    }
}
